package com.pxmeta.party.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.pxmeta.common.exception.ServiceException;
import com.pxmeta.common.utils.agora.AgoraClient;
import com.pxmeta.party.entity.Party;
import com.pxmeta.party.entity.PartyUser;
import com.pxmeta.party.entity.dto.CreatePartyDTO;
import com.pxmeta.party.entity.enums.PartyUserRoleEnum;
import com.pxmeta.party.entity.enums.UserLiveStatusEnum;
import com.pxmeta.party.entity.vo.ChannelUserVO;
import com.pxmeta.party.mapper.PartyMapper;
import com.pxmeta.party.mapper.PartyUserMapper;
import com.pxmeta.party.service.IPartyService;
import com.pxmeta.party.service.IPartyUserService;
import com.pxmeta.user.entity.User;
import com.pxmeta.user.entity.vo.UserVO;
import com.pxmeta.user.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 语聊派对表 服务实现类
 * </p>
 *
 * @author wcl
 * @since 2022-06-25
 */
@Service
public class PartyUserServiceImpl extends ServiceImpl<PartyUserMapper, PartyUser> implements IPartyUserService {

    @Autowired
    private IUserService userService;

    @Autowired
    private AgoraClient agoraClient;


    @Override
    public List<UserVO> getPartyUsers(String id) {

        LambdaQueryWrapper<PartyUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PartyUser::getPartyId, id)
                .orderByAsc(PartyUser::getRole)
                .last("limit 3");

        List<PartyUser> list = this.list(wrapper);
        List<Long> userIds = list.stream().map(PartyUser::getUserId).collect(Collectors.toList());
        return userService.getUserVOList(userIds);
    }

    @Override
    public List<PartyUser> getByUserIds(List<Long> userIds) {
        if(CollectionUtil.isEmpty(userIds)){
            return Lists.newArrayList();
        }
        LambdaQueryWrapper<PartyUser> wrapper = Wrappers.lambdaQuery();
        wrapper.in(PartyUser::getUserId, userIds)
                .orderByDesc(PartyUser::getCreateTime);
        return this.list(wrapper);
    }

    @Override
    public String getUserPartyId(Long userId) {

        User user = userService.getById(userId);

//        LambdaQueryWrapper<PartyUser> wrapper = Wrappers.lambdaQuery();
//        wrapper.eq(PartyUser::getUserId, userId);
//        List<PartyUser> list = this.list(wrapper);

        return null;

    }

    @Override
    public PartyUser getPartyUser(String partyId, String adminUsername) {
        User user = userService.getByUsername(adminUsername);
        LambdaQueryWrapper<PartyUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PartyUser::getPartyId,partyId)
                .eq(PartyUser::getUserId,user.getId());

        return this.getOne(wrapper);
    }

    @Override
    public List<User> getAdminUsers(String partyId) {
        LambdaQueryWrapper<PartyUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PartyUser::getPartyId, partyId)
                .eq(PartyUser::getRole, PartyUserRoleEnum.ADMIN.getRole());
        List<PartyUser> list = this.list(wrapper);
        List<Long> userIds = list.stream().map(PartyUser::getUserId).collect(Collectors.toList());
        if (userIds.isEmpty()){
            return Lists.newLinkedList();
        }
        List<User> users = userService.listByIds(userIds);
        List<User> finalList = Lists.newLinkedList();
        ChannelUserVO vo = agoraClient.getUsers(partyId).toJavaObject(ChannelUserVO.class);
        for (User user : users) {
            //再校验一次
            if (vo.getChannelExist()) {
                List<String> broadcaseters = vo.getBroadcasters().stream().map(l -> l.toString()).collect(Collectors.toList());
                List<String> audiences = vo.getAudience().stream().map(l -> l.toString()).collect(Collectors.toList());
                LinkedList<String> allUsernames = Lists.newLinkedList();
                allUsernames.addAll(broadcaseters);
                allUsernames.addAll(audiences);
                if (allUsernames.contains(user.getUsername())){
                    finalList.add(user);
                }
            }
        }

        return finalList;
    }

    @Override
    public Set<String> getVisitPartyIds(Long userId) {
        LambdaQueryWrapper<PartyUser> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(PartyUser::getUserId,userId)
                .orderByDesc(PartyUser::getCreateTime)
                .last("limit 100");

        return this.list(wrapper).stream().map(PartyUser::getPartyId).collect(Collectors.toSet());
    }
}
